package com.hust360.percent.pojo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.pojo.Discount.DiscountDAO;
import com.hust360.percent.utils.StorageUtils;

public class DiscountDAOImpl implements DiscountDAO {

	SimpleDateFormat format = StorageUtils.simpleDateFormat;

	public DiscountDAOImpl(Application application) {
	}

	/**
	 * 通过主键来返回一个DiscountInfo, 如果未找到，返回null
	 */
	@Override
	public Discount findById(long discountId) {
		Discount info = null;

		Cursor c;
		SQLiteDatabase db = PercentApplication.localDB;
		synchronized (db) {
			c = db.rawQuery("SELECT * FROM discount WHERE id=?",
					new String[] { discountId + "" });
		}

		if (c.moveToFirst()) {
			info = new DiscountLocal();
			info.describe = c.getString(c.getColumnIndex(Discount.DESCRIBE));
			info.discount = c.getInt(c.getColumnIndex(Discount.DISCOUNT));
			info.disctype = c.getInt(c.getColumnIndex(Discount.DISCTYPE));

			try {
				info.enddate = format.parse(c.getString(c
						.getColumnIndex(Discount.END_DATE)));
			} catch (Exception e) {
				info.enddate = null;
				e.printStackTrace();
			}

			info.id = c.getLong(c.getColumnIndex(Discount.ID));

			try {
				info.moddate = format.parse(c.getString(c
						.getColumnIndex(Discount.MOD_DATE)));
			} catch (Exception e) {
				info.moddate = null;
				e.printStackTrace();
			}

			info.shopid = c.getLong(c.getColumnIndex(Discount.SHOP_ID));

			try {
				info.startdate = format.parse(c.getString(c
						.getColumnIndex(Discount.START_DATE)));
			} catch (Exception e) {
				info.startdate = null;
				e.printStackTrace();
			}
		}
		c.close();
		return info;
	}

	/**
	 * 将info对象保存到数据库中
	 */
	@Override
	public void save(Discount info) {
		ContentValues values = new ContentValues();
		values.put(Discount.DESCRIBE, info.describe);
		values.put(Discount.DISCOUNT, info.discount);
		values.put(Discount.DISCTYPE, info.disctype);

		try {
			String str = format.format(info.enddate);
			values.put(Discount.END_DATE, str);
		} catch (Exception e) {
			e.printStackTrace();
		}

		values.put(Discount.ID, info.id);

		try {
			String str = format.format(info.moddate);
			values.put(Discount.MOD_DATE, str);
		} catch (Exception e) {
			e.printStackTrace();
		}

		values.put(Discount.SHOP_ID, info.shopid);

		try {
			String str = format.format(info.startdate);
			values.put(Discount.START_DATE, str);
		} catch (Exception e) {
			e.printStackTrace();
		}

		SQLiteDatabase db = PercentApplication.localDB;
		synchronized (db) {
			db.insertWithOnConflict(Discount.TABLE, null, values,
					SQLiteDatabase.CONFLICT_REPLACE);
		}
	}

	public List<Discount> findByArgs(String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {

		SimpleDateFormat format = StorageUtils.simpleDateFormat;

		ArrayList<Discount> list = null;
		SQLiteDatabase db = PercentApplication.localDB;
		Cursor c;
		synchronized (db) {
			c = db.query(Discount.TABLE, columns, selection, selectionArgs,
					groupBy, having, orderBy);
		}

		list = new ArrayList<Discount>(c.getCount());
		Discount info;
		while (c.moveToNext()) {
			info = new DiscountLocal();
			info.describe = c.getString(c.getColumnIndex(Discount.DESCRIBE));
			info.discount = c.getInt(c.getColumnIndex(Discount.DISCOUNT));
			info.disctype = c.getInt(c.getColumnIndex(Discount.DISCTYPE));

			try {
				info.enddate = format.parse(c.getString(c
						.getColumnIndex(Discount.END_DATE)));
			} catch (Exception e) {
				info.enddate = null;
				e.printStackTrace();
			}

			info.id = c.getLong(c.getColumnIndex(Discount.ID));

			try {
				info.moddate = format.parse(c.getString(c
						.getColumnIndex(Discount.MOD_DATE)));
			} catch (Exception e) {
				info.moddate = null;
				e.printStackTrace();
			}

			info.shopid = c.getLong(c.getColumnIndex(Discount.SHOP_ID));

			try {
				info.startdate = format.parse(c.getString(c
						.getColumnIndex(Discount.START_DATE)));
			} catch (Exception e) {
				info.startdate = null;
				e.printStackTrace();
			}
			list.add(info);
		}
		c.close();
		return list;
	}

	@Override
	public List<Discount> findWithMap(Map<String, String> map) {
		String value;
		List<String> columns = Arrays.asList(Discount.columns);
		StringBuffer buff = new StringBuffer();
		List<Discount> list;
		Discount dsc;
		Cursor c;

		Set<String> keys = map.keySet();
		for (String key : keys) {
			value = map.get(key);
			if (columns.contains(key) && value != null) {
				buff.append(key + "='" + value + "' AND ");
			}
		}
		buff.append("'1'='1'");

		synchronized (PercentApplication.localDB) {
			c = PercentApplication.localDB.rawQuery(
					"SELECT * FROM discount WHERE " + buff.toString(), null);
		}
		list = new ArrayList<Discount>(c.getCount());
		while (c.moveToNext()) {
			dsc = new DiscountLocal();
			dsc.describe = c.getString(c.getColumnIndex(Discount.DESCRIBE));
			dsc.discount = c.getInt(c.getColumnIndex(Discount.DISCOUNT));
			dsc.disctype = c.getInt(c.getColumnIndex(Discount.DISCTYPE));

			try {
				dsc.enddate = format.parse(c.getString(c
						.getColumnIndex(Discount.END_DATE)));
			} catch (Exception e) {
				dsc.enddate = null;
				e.printStackTrace();
			}

			dsc.id = c.getLong(c.getColumnIndex(Discount.ID));

			try {
				dsc.moddate = format.parse(c.getString(c
						.getColumnIndex(Discount.MOD_DATE)));
			} catch (Exception e) {
				dsc.moddate = null;
				e.printStackTrace();
			}

			dsc.shopid = c.getLong(c.getColumnIndex(Discount.SHOP_ID));

			try {
				dsc.startdate = format.parse(c.getString(c
						.getColumnIndex(Discount.START_DATE)));
			} catch (Exception e) {
				dsc.startdate = null;
				e.printStackTrace();
			}
			list.add(dsc);
		}
		c.close();
		return list;
	}

}
